<template>
    <el-dialog
        width="30%"
        title=""
        top="30px"
        :visible.sync="innerVisible"
        :modal="false"
        class="confirm-pop"
        :close-on-click-modal="false"
        @closed="handleCancel"
    >
        <div slot="title">
            <span class="el-icon-warning"></span> 是否确认
        </div>
        <div class="con">
            <span v-if="signType == 'single'">{{showOrder[orderInd].title}} 医废 共 {{showOrder[orderInd].weight.toFixed(2)}}kg，{{showOrder[orderInd].num}}袋</span>
            <span v-else>所有科室 医废 共 {{allWeight.toFixed(2)}}kg，{{allNum}}袋</span>
            <br/><br/>
            <p>请填写{{$root.$data.is_szy ? '工号':'全名'}}(必填)</p>
            <input type="text" v-model="requiredName" @change="handleGetName">
            <div class="szy-confirm-info" v-if="$root.$data.is_szy">
                <span class="gary">工号姓名：</span>
                <span class="gary" v-if="!requiredNameSzy">暂无数据；请输入工号，回车键（enter）获取信息</span>
                <span v-else>{{requiredNameSzy}}</span>
            </div>

        </div>
        <div class="handle-box">
            <el-button type="info" @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSure" :disabled="!isHandle">确认</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {confirmSignUrl, wasteFindNameUrl} from'../../common/ajax'
export default {
    props:{
        signType:{
            type: String,
            default: 'single'
        },
        originOrder:{
            type: Object
        },
        orders:{
            type: Array
        },
        orderInd:{
            type: Number,
            default: 0
        }
    },
    data(){
        return{
            requiredName:'',
            requiredNameSzy:'',
            showOrder:[],
            isHandle:true,
            allNum: 0,
            allWeight: 0,
            innerVisible: true
        }
    },
    created(){
        this.showOrder = this.orders
        this.generateData()
    },
    watch:{
        'orders'(){
            this.showOrder = this.orders
            this.generateData()
        }
    },
    methods:{
        handleGetName(){
            if(!this.$root.$data.is_szy) return
            if(!this.requiredName.trim()){
                this.requiredNameSzy = ""
                return
            }
            jQuery.ajax({
                url: wasteFindNameUrl,
                type: 'get',
                data:{code:this.requiredName.trim()},
                success: function(res){
                    if(res.success && res.data && res.data != "0") {
                        this.requiredNameSzy = this.requiredName.trim()+res.data
                    }else {
                        this.requiredNameSzy = ""
                    }
                }.bind(this)
            })
        },
        generateData(){
            this.allNum = 0;
            this.allWeight = 0;
            if(this.signType == 'multiple'){
                this.showOrder.forEach(v => {
                    let currNum = v.ks.reduce((prev, curr) => prev + curr.num, 0)
                    let currWeight = v.ks.reduce((prev, curr) => prev + curr.weight, 0)

                    this.allNum += currNum
                    this.allWeight += currWeight
                })
                // this.allNum = this.showOrder.ks.reduce((prev, curr) => prev + curr.num, 0)
                // this.allWeight = this.showOrder.ks.reduce((prev, curr) => prev + curr.weight, 0)
            }
        },
        handleCancel(){
            this.innerVisible = false;
            this.$emit('handleCancel')
        },
        handleSure(){
            if(this.requiredName.trim() == '' || (this.$root.$data.is_szy && !this.requiredNameSzy.trim())){
                this.$Message({
                    showClose: true,
                    message: this.$root.$data.is_szy?'填写工号获取信息后才能提交':'填写工号或姓名才能提交',
                    type: 'error'
                });
                return;
            }
            if(this.isHandle){
                this.isHandle = false;
                let ids = {ids:[]}
                if(this.signType == 'single'){
                    this.showOrder[this.orderInd].lis.forEach((val,ind)=>{
                        ids.ids.push(Number(val.id))
                    })
                }else{
                    this.showOrder.forEach(v => {
                        v.typeL.forEach(v2 => {
                            v2.lis.forEach(v3 => {   
                                ids.ids.push(Number(v3.id))
                            })
                        })
                    })
                }
                
                let obj = {
                    step1_uinfo: this.$root.$data.is_szy ? this.requiredNameSzy :this.requiredName,
                    ids: ids.ids
                }
                jQuery.ajax({
                    url: confirmSignUrl,
                    type: 'POST',
                    data: JSON.stringify(obj),
                    contentType: 'application/json;charset=UTF-8',
                    success: function(res){
                        this.isHandle = true;
                        if(!res.success) {
                            throwErrorTips('医废确认失败',res.msg)
                            return
                        }
                        // this.innerVisible = false;
                        if(this.showOrder.length > 1){
                            this.$emit('sure',0,this.originOrder)
                            this.handleCancel();
                        }else {
                            this.$emit('sure',1,this.originOrder)
                            this.handleCancel();
                        }
                        this.$Message({
                            showClose: true,
                            message: '医废确认成功',
                            type: 'success'
                        });

                    }.bind(this),
                    error:function(res){
                        // this.innerVisible = false;
                        this.handleCancel();
                        this.isHandle = true;
                        throwErrorTips('医废确认失败',res)
                    }.bind(this)
                })
            }
        }
    }
}
</script>

<style>

</style>